package com.legendframework.core.utils;

import org.bukkit.Bukkit;

import java.util.stream.IntStream;

/**
 * 服务器版本类型
 */
public enum BukkitVersion {

    SERVER8("v1_8_R","1.8"),
    SERVER9("v1_9_R","1.9"),
    SERVER10("v1_10_R","1.10"),
    SERVER11("v1_11_R","1.11"),
    SERVER12("v1_12_R","1.12"),
    SERVER13("v1_13_R","1.13"),
    SERVER14("v1_14_R","1.14"),
    SERVER15("v1_15_R","1.15"),
    SERVER16("v1_16_R","1.16"),
    SERVER17("v1_17_R","1.17"),
    SERVER18("v1_18_R","1.18"),
    SERVER19("v1_19_R","1.19"),
    ;

    /**
     * 服务器版本号
     */
    private String serverVersion;

    /**
     * cms版本号
     */
    private String cmsVersion;

    /**
     * 当前服务器版本号
     */
    private static int[] versionSplit = new int[3];

    static {
        String version = Bukkit.getBukkitVersion().split("-")[0].replace(" ", "");
        String[] strSplit = version.split("[.]");
        IntStream.range(0, strSplit.length).forEach(i -> versionSplit[i] = Integer.valueOf(strSplit[i]));
    }

    /**
     * 根据服务器版本号获取
     * @param serverVersion 服务器版本号
     * @return 类型
     */
    public static BukkitVersion formServerVersion(String serverVersion){
        for (BukkitVersion bukkitVersionEnum : values()) {
            if (bukkitVersionEnum.getServerVersion().equals(serverVersion)) {
                return bukkitVersionEnum;
            }
        }
        return null;
    }

    /**
     * 获取版本号
     * 1.12.2
     * 索引获取
     * 0 1  2
     * @param index 索引
     * @return
     */
    public static int getVersionInt(int index){
        if (index >= versionSplit.length) {
            throw new IllegalArgumentException("获取版本号不正确");
        }
        return versionSplit[index];
    }

    public String getServerVersion() {
        return serverVersion;
    }

    public String getCmsVersion(String rVersion) {
        return cmsVersion+rVersion;
    }

    BukkitVersion(String cmsVersion , String serverVersion){
        this.cmsVersion = cmsVersion;
        this.serverVersion = serverVersion;
    }

    /**
     * 获取当前运行的服务器的版本
     * @return 当前服务器的版本
     */
    public static BukkitVersion getBukkitVersion(){
        String[] split = Bukkit.getBukkitVersion().split("-")[0].split("\\.");
        return BukkitVersion.formServerVersion(split[0]+"."+(split[1]));
    }
}
